package com.easy.system.controller;

import com.easy.system.service.ContextService;
import com.easy.system.util.HibernateBeanReflectUtil;
import com.easy.system.util.KeyValueDto;
import flexjson.JSONSerializer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: sunguangzhu
 * Date: 13-8-6
 * Time: 下午4:21
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class ContextController extends AbstractController {

    @Resource
    private ContextService contextService;

    @ResponseBody
    @RequestMapping
    public void getKeyValueDtos(HttpServletResponse response, String className) throws ClassNotFoundException, IOException, SQLException {
        Class clazz = Class.forName(className);
        String tableName = HibernateBeanReflectUtil.getTableName(clazz);
        List<KeyValueDto> keyValueDtoList = this.contextService.getKeyValueDtos(tableName);
        String results = new JSONSerializer().exclude("*.class").serialize(keyValueDtoList);
        super.writeToClient(response, results);
    }
}
